const mongoose = require('mongoose')
mongoose.Promise = global.Promise;
const db = require('./config/key').mongoURL3;
mongoose.connect(db, { useNewUrlParser: true })
    .then(() => {
        console.log("数据库连接成功");
    })
    .catch(err => {
        console.log(err);
    })
const Schema = mongoose.Schema;

const UserSchema = new Schema({
    title: {
        type: String,
    }
})
const User = mongoose.model('User', UserSchema)
    // 查找
async function get(val) {
    if (val) {

        const da = await User.findOne({ title: val })
        console.log(da);
    } else {
        const data = await User.find()
        console.log(data);
    }
}
// 添加数据
function add(val) {
    User({ title: val }).save().then(user => console.log(user))
}
// add("webpack")
// async await 添加
async function add1(val) {
    const data = await User({ title: val }).save()
    console.log(data);

}
// add1("H5")
// User.find().then(user => console.log(user))
// 修改 
async function edit(val, val1) {
    const data = await User.findOneAndUpdate({ title: val }, { $set: val1 }, { new: true })
    console.log(data);
}
// edit('webpack', 'H5CS3')
// get('vue')
// get()
async function dele(val) {
    if (Array.isArray(val)) {
        const data = await User.findOneAndDelete({ title: val })
        console.log(data);
    } else {
        const data = User.deleteMany({ _id: { $in: itemCheckLists } })
        console.log(data);
    }
}
// dele('vue')
// get()